﻿//----------------------------------------------------------------
//CodeArt.
//
//Descrpton:
//
//Author:jianyi0115@163.com  
//Created: 2009-10-27
//
//修订记录: 
//
//----------------------------------------------------------------
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Microsoft.SharePoint;
using System.Collections.Specialized;
using System.Collections;
using System.Web;
using System.Web.Caching;

namespace CodeArt.SharePoint.MultiLanSupport
{
    /// <summary>
    /// 语言映射管理
    /// </summary>
    class LanMapper
    {
        /// <summary>
        /// 是否启用多语言
        /// </summary>
        /// <param name="curWeb"></param>
        /// <returns></returns>
        public static bool EnableLanTrans(SPWeb curWeb)
        {
            object obj = HttpContext.Current.Application["CodeArt_EnableTrans_" + curWeb.ID];
            if( obj == null )
                return true ;
            else
                return false ;
        }
        
         DataTable GetData(SPWeb curWeb)
        {
            try
            {
                SPList list = curWeb.Lists["MultiLanConfig"];
                return list.Items.GetDataTable();
            }
            catch (ArgumentException)
            {
                //若列表不存在则记录下来，下次请求就无需再进行翻译
                HttpContext.Current.Application["CodeArt_EnableTrans_" + curWeb.ID] = false;
                return null;
            }
        }

        private static readonly StringDictionary _LanMap = new StringDictionary();

        string GetLanKeyMap(string lan)
        {
            if (_LanMap.ContainsKey(lan))
                return _LanMap[lan];
            else
            {
                string map = System.Configuration.ConfigurationManager.AppSettings[lan];
                if (!String.IsNullOrEmpty(map))
                {
                    _LanMap[lan] = map;
                    return map;
                }else{
                    _LanMap[lan] = lan;
                    return lan;
                }
            }
        }

        private static readonly Hashtable _LanKeys = new Hashtable();

        /// <summary>
        /// 获取语言key
        /// </summary>
        /// <returns></returns>
        public Hashtable GetLanKeys()
        {
            SPWeb curWeb = SPContext.Current.Web;
            string cacheKey = "MultiLanCache_" + curWeb.ID + "_LanKeys" ;

            Hashtable t = HttpContext.Current.Cache.Get(cacheKey) as Hashtable;
            if (t == null)
            {
                DataTable table = GetData(curWeb);
                t = new Hashtable();
                if (table != null)
                {
                    foreach (DataRow r in table.Rows)
                    {
                        t[r["Title"].ToString()] = null;
                    }
                }
                HttpContext.Current.Cache[cacheKey] = t;
            }
            return t;
        }

        /// <summary>
        /// 获取某种语言的映射
        /// </summary>
        /// <param name="lan"></param>
        /// <returns></returns>
        public Hashtable GetLanMap(string lan)
        {
            string lanKey = this.GetLanKeyMap(lan);
            SPWeb curWeb = SPContext.Current.Web;

            DataTable table = GetData(curWeb);
            Hashtable t = new Hashtable();
            if (table != null)
            {
                if (!table.Columns.Contains(lan))
                    throw new Exception(String.Format("Language Config for [{0}] no exist", lan));
               
                foreach (DataRow r in table.Rows)
                {
                    t[r["Title"].ToString()] = r[lanKey].ToString();
                }
            }
            return t;
        }

        /// <summary>
        /// 获取某种语言的映射,缓存处理
        /// </summary>
        /// <param name="lan"></param>
        /// <returns></returns>
        public Hashtable GetCachedLanMap(string lan)
        {
            string lanKey = this.GetLanKeyMap(lan);
            SPWeb curWeb = SPContext.Current.Web;
            string cacheKey = "MultiLanCache_" + curWeb.ID + "_" + lanKey;

            Hashtable t = HttpContext.Current.Cache.Get(cacheKey) as Hashtable;
            if (t == null)
            {
                t = new Hashtable();
                DataTable table = GetData(curWeb);
                if (table != null)
                {
                    if (!table.Columns.Contains(lan))
                        throw new Exception(String.Format("Language Config for [{0}] no exist", lan));

                    foreach (DataRow r in table.Rows)
                    {
                        t[r["Title"].ToString()] = r[lanKey].ToString();
                    }
                }

                HttpContext.Current.Cache.Insert(cacheKey, t, null, DateTime.Now.AddMinutes(20), TimeSpan.Zero);    
            
            }
            return t;
        }
               
    }
}
